package com.scalar.chiptrack.commons;

import com.scalar.chiptrack.utils.ConfigManager;

import java.io.Serializable;
import java.io.File;
import java.util.Date;
import java.net.URLEncoder;

/**
 * User: Sujan
 */
public class AttachedFile implements Serializable {
    private int id;
    private String fileName;
    private String physicalFileName; //File name stored on the disk
    private String comments;
    private Date timeStamp;
    private String fileGroup;
    private String fileSubGroup;
    private boolean deleted;
    private String attachedByUserId;

    public AttachedFile() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Date getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Date timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getFileGroup() {
        return fileGroup;
    }

    public void setFileGroup(String fileGroup) {
        this.fileGroup = fileGroup;
    }

    public String getFileSubGroup() {
        return fileSubGroup;
    }

    public void setFileSubGroup(String fileSubGroup) {
        this.fileSubGroup = fileSubGroup;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getPhysicalFileName() {
        return physicalFileName;
    }

    public void setPhysicalFileName(String physicalFileName) {
        this.physicalFileName = physicalFileName;
    }

    public String getAttachedByUserId() {
        return attachedByUserId;
    }

    public void setAttachedByUserId(String attachedByUserId) {
        this.attachedByUserId = attachedByUserId;
    }

    public String getFileLinkEncoded() throws Exception {
        String enc = "UTF-8";
        String directoryPath = ConfigManager.getProcessDocWebFilePath() + "/" + URLEncoder.encode(getFileGroup(),enc);
        if(getFileSubGroup() != null && getFileSubGroup().trim().length()!=0) {
            directoryPath += "/" + URLEncoder.encode(getFileSubGroup(),enc);
        }
        String link = directoryPath + "/" + URLEncoder.encode(getPhysicalFileName(),enc);
        link = link.replaceAll("\\+","%20");
        return link;
    }

    public String getAbsolutePath() {
        String directoryPath = ConfigManager.getProcessDocFilePath() + File.separator + getFileGroup();
        if(getFileSubGroup() != null && getFileSubGroup().trim().length()!=0) {
            directoryPath += File.separator + getFileSubGroup();
        }
        return directoryPath + File.separator + getPhysicalFileName();
    }
}
